﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelDragLove : MonoBehaviour
{
    public GameLevelItem gamLevelItem;
    public Text lText;
    public Image lImage;
    public DragObjectY dragL;
    private float tempLposY;

    public Text oText;
    public Image oImage;
    public DragObjectY dragO;
    private float tempOposY;

    public Text vText;
    public Image vImage;
    public DragObjectY dragV;
    private float tempVposY;

    public Text eText;
    public Image eImage;
    public DragObjectY dragE;
    private float tempEposY;

    public float maxPosY;
    public float minPosY;


    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("oimage pos y " + oImage.transform.position.y);
        dragL.endDragEvent.AddListener(() => { TriggerResult(); });
        dragO.endDragEvent.AddListener(() => { TriggerResult(); });
        dragV.endDragEvent.AddListener(() => { TriggerResult(); });
        dragE.endDragEvent.AddListener(() => { TriggerResult(); });


        dragL.startDragEvent.AddListener(() =>
        {
            tempOposY = oImage.transform.position.y;
        });

        dragO.startDragEvent.AddListener(() =>
        {
            tempLposY = lImage.transform.position.y;
            tempVposY = vImage.transform.position.y;
        });

        dragV.startDragEvent.AddListener(() =>
        {
            tempEposY = eImage.transform.position.y;
        });

        dragE.startDragEvent.AddListener(() =>
        {
            tempLposY = lImage.transform.position.y;
            tempVposY = vImage.transform.position.y;
        });
    }

    // Update is called once per frame
    void Update()
    {


    }

    private void FixedUpdate()
    {
        lText.rectTransform.anchoredPosition = -1 * lImage.rectTransform.anchoredPosition;
        oText.rectTransform.anchoredPosition = -1 * oImage.rectTransform.anchoredPosition;
        vText.rectTransform.anchoredPosition = -1 * vImage.rectTransform.anchoredPosition;
        eText.rectTransform.anchoredPosition = -1 * eImage.rectTransform.anchoredPosition;
        if (dragL.isDrag)
        {
            oImage.transform.position = new Vector3(oImage.transform.position.x,
                GetFinalPosY(tempOposY - (dragL.target.position.y - dragL.dragStartPosY)),
                oImage.transform.position.z);
        }

        if (dragO.isDrag)
        {
            lImage.transform.position = new Vector3(lImage.transform.position.x,
                GetFinalPosY(tempLposY - (dragO.target.position.y - dragO.dragStartPosY)),
                lImage.transform.position.z);

            vImage.transform.position = new Vector3(vImage.transform.position.x,
                GetFinalPosY(tempVposY - (dragO.target.position.y - dragO.dragStartPosY)),
                vImage.transform.position.z);
        }

        if (dragV.isDrag)
        {
            eImage.transform.position = new Vector3(eImage.transform.position.x,
                GetFinalPosY(tempEposY + (dragV.target.position.y - dragV.dragStartPosY)),
                eImage.transform.position.z);
        }

        if (dragE.isDrag)
        {
            lImage.transform.position = new Vector3(lImage.transform.position.x,
                GetFinalPosY(tempLposY + (dragE.target.position.y - dragE.dragStartPosY)),
                lImage.transform.position.z);

            vImage.transform.position = new Vector3(vImage.transform.position.x,
                GetFinalPosY(tempVposY + (dragE.target.position.y - dragE.dragStartPosY)),
                vImage.transform.position.z);
        }




    }

    private float GetFinalPosY(float tempPosY)
    {
        if (tempPosY < minPosY)
            tempPosY = minPosY;
        else if (tempPosY > maxPosY)
            tempPosY = maxPosY;
        return tempPosY;
    }

    private void TriggerResult()
    {
        if (lImage.transform.position.y < minPosY+5 &&
            oImage.transform.position.y < minPosY + 5 &&
            vImage.transform.position.y < minPosY + 5 &&
            eImage.transform.position.y < minPosY + 5)
        {
            dragL.enabled = false;
            dragO.enabled = false;
            dragV.enabled = false;
            dragE.enabled = false;
            gamLevelItem.TriggerResult(true);
        }
    }
}

